package com.example.study.plus.mybatis_plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.study.config.JsonResult;
import com.example.study.plus.mybatis_plus.entity.ChatMessages;
import com.example.study.plus.mybatis_plus.mapper.ChatMessagesMapper;
import com.example.study.plus.mybatis_plus.service.ChatMessagesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 聊天信息表 服务实现类
 * </p>
 *
 * @author 路飞4
 * @since 2023-08-29
 */
@Service
public class ChatMessagesServiceImpl extends ServiceImpl<ChatMessagesMapper, ChatMessages> implements ChatMessagesService {

    @Override
    public int sendAMessage(int sender_id, int receiver_id,String message,boolean pushed) {
        ChatMessages chatMessages=new ChatMessages();
        chatMessages.setSenderId(sender_id);
        chatMessages.setReceiverId(receiver_id);
        chatMessages.setMessage(message);
        chatMessages.setPushed(pushed);
        return baseMapper.insert(chatMessages);
    }

    @Override
    public int sendAMessage(ChatMessages chatMessages) {
        return baseMapper.insert(chatMessages);
    }


    @Override
    public Page<ChatMessages> queryTheListOfMessages(int sender_id, int receiver_id,int page,int pageSize) {

//        //分页参数
//        Page<SchoolStudent> rowPage = new Page(page, pageSize);
//
//        //queryWrapper组装查询where条件
//        LambdaQueryWrapper<SchoolStudent> queryWrapper = new LambdaQueryWrapper<>();
//        rowPage = this.baseMapper.selectPage(rowPage, queryWrapper);
//        return rowPage;

        Page<ChatMessages> rowPage = new Page<>(page, pageSize);

        rowPage.addOrder(OrderItem.desc("createTime"));
        QueryWrapper<ChatMessages> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("sender_id",sender_id).eq("receiver_id",receiver_id);
        queryWrapper.or();
        queryWrapper.eq("sender_id",receiver_id).eq("receiver_id",sender_id);
//        queryWrapper.orderByAsc("createTime");
        return baseMapper.selectPage(rowPage,queryWrapper);
    }
}
